(function() {

	var animationDeferred = Ender.Game.RequestNotifiers.animationRequestNotifier = $.Deferred();
	var itemsTypesDeferred = Ender.Game.RequestNotifiers.itemTypeRequestNotifier = $.Deferred();
	var itemsDeferred = Ender.Game.RequestNotifiers.itemRequestNotifier = $.Deferred();
	var choicesDeferred = Ender.Game.RequestNotifiers.choiceRequestNotifier = $.Deferred();
	var charactersDeferred = Ender.Game.RequestNotifiers.characterRequestNotifier = $.Deferred();
	var arenaDeferred = Ender.Game.RequestNotifiers.arenaRequestNotifier = $.Deferred();
	var featsDeferred = Ender.Game.RequestNotifiers.featRequestNotifier = $.Deferred();
	var obstaclesDeferred = Ender.Game.RequestNotifiers.obstacleRequestNotifier = $.Deferred();
	var enemiesDeferred = Ender.Game.RequestNotifiers.enemyRequestNotifier = $.Deferred();

	var simpleFeatsDeferred = $.Deferred();
	var featTemplatesDeferred = $.Deferred();

	function fetchEnemies() {
		$.getJSON('data/enemies', function(enemies) {
			Ender.Game.Repositories.RepositoryFactory.createEnemyRepository(enemies);
			enemiesDeferred.resolve();
		});
	}
	function fetchObstacles() {
		$.getJSON('data/obstacles', function(obstacles) {
			Ender.Game.Repositories.RepositoryFactory.createObstacleRepository(obstacles);
			obstaclesDeferred.resolve();
		});
	}
	function fetchFeatTemplates() {
		$.getJSON('data/featTemplates', function(featTemplates) {
			Ender.Game.Repositories.RepositoryFactory.applyFeatTemplates(featTemplates);
			featTemplatesDeferred.resolve();
		});
	}
	;
	function fetchCharacters() {
		$.getJSON('data/characters', function(characters) {
			Ender.Game.Repositories.RepositoryFactory.createCharacterRepository(characters);
			charactersDeferred.resolve();
		});
	}
	;
	function fetchItems() {
		$.getJSON('data/items', function(items) {
			Ender.Game.Repositories.RepositoryFactory.createItemRepository(items);
			itemsDeferred.resolve();
		});
	}
	function fetchFeats() {
		$.getJSON('data/feats', function(featData) {
			Ender.Game.Repositories.RepositoryFactory.createFeatRepository(featData);
			simpleFeatsDeferred.resolve();
		});
	}
	function fetchItemTypes() {
		$.getJSON('data/itemTypes', function(itemTypeData) {
			Ender.Game.Repositories.RepositoryFactory.createItemTypeRepository(itemTypeData);
			itemsTypesDeferred.resolve();
		});
	}
	function fetchAnimations() {
		$.getJSON('data/animations', function(animationData) {
			Ender.Game.Repositories.RepositoryFactory.createAnimationRepository(animationData);
			animationDeferred.resolve();
		});
	}
	function fetchStartingChoices() {
		$.getJSON('data/choiceTree', function(choiceTree) {
			Ender.Game.Repositories.RepositoryFactory.createChoiceRepository(choiceTree);
			choicesDeferred.resolve();
		});
	}
	function fetchArenas() {
		$.getJSON('data/arenas', function(arenas) {
			Ender.Game.Repositories.RepositoryFactory.createArenaRepository(arenas);
			arenaDeferred.resolve();
		});
	}

	// characters need animations, items and feats
	animationDeferred.done(function() {
		itemsDeferred.done(function() {
			featsDeferred.done(function() {
				fetchCharacters();
			});
		});
	});

	// feats are done when both individual feats and feat templates are loaded
	simpleFeatsDeferred.done(function() {
		featTemplatesDeferred.done(function() {
			featsDeferred.resolve();
		});
	});

	// items need item types
	itemsTypesDeferred.done(function() {
		fetchItems();
	});

	// feat templates need item types
	itemsTypesDeferred.done(function() {
		fetchFeatTemplates();
	});

	// arenas need obstacles and enemies
	enemiesDeferred.done(function() {
		obstaclesDeferred.done(function() {
			fetchArenas();
		});
	});

	// enemies need everything characters need
	charactersDeferred.done(function() {
		fetchEnemies();
	});

	fetchObstacles();
	fetchFeats();
	fetchItemTypes();
	fetchAnimations();
	fetchStartingChoices();

	$(function() {
		if (Ender.Game.Settings.atWork) {
			$('#container').addClass('at-work');
		}
	});

})();

(function() {
	String.prototype.capitalize = function() {
		return this.charAt(0).toUpperCase() + this.slice(1);
	};
	Array.prototype.pushAll = function(arr) {
		this.push.apply(this, arr);
	};
	Number.prototype.roundTo = function(to) {
	    return Math.round(this / to) * to;
	};
}());

(function() {
	Ender.Game.Popups = {};

	var container = undefined;
	$(function() {
		container = $("#popup");
		if (container.dialog) {
			container.dialog({
				autoOpen : false
			});
		}
	});

	Ender.Game.Popups.showMessage = function(message, callback) {

		container.html(message).dialog({
			autoOpen : true,
			modal : true,
			dialogClass : "game-dialog",
			resizable : false,
			width : 400,
			close : function() {
				if (callback) {
					callback();
				}
			}
		});
	};

	Ender.Game.Popups.showMessageWithOptions = function(message, options) {

		var dialogOptions = $.extend({
			autoOpen : true,
			modal : true,
			dialogClass : "game-dialog",
			resizable : false,
		}, options);

		container.html(message).dialog(dialogOptions);
	};

	Ender.Game.Popups.clearPopup = function() {
		if (container.dialog("isOpen"))
			container.dialog('close');
	};
}());
